home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / AECUR100.ARJ / NEWWIN.C < prev    next >
C/C++ Source or Header  |  1990-03-08  |  4KB  |  159 lines

  1. /*------------------------------------------------------------
  2.  * 
  3.  *  newwin.c
  4.  * 
  5.  *  copyright (c) 1987,88,89,90 J. Alan Eldridge
  6.  *
  7.  *  create new windows and subwindows
  8.  * 
  9.  *  NOTE: SUB-WINDOWS MUST BE WHOLLY CONTAINED WITHIN THEIR
  10.  *  PARENT WINDOWS OR ALLOCATION FAILS!
  11.  * 
  12.  *----------------------------------------------------------*/
  13.  
  14. #include "curses.h"
  15.  
  16. WINDOW *
  17. newwin(r,c,y,x)
  18. {
  19.     WINDOW *win;
  20.     void *ptrs[4];
  21.     int row;
  22.     VIDCHR fillch;
  23.     
  24.     if (r == 0)
  25.         r = LINES - y;
  26.     if (c == 0)
  27.         c = COLS - x;
  28.                 
  29.     ptrs[0] = calloc(1, sizeof(WINDOW));
  30.     ptrs[1] = calloc(r, sizeof(VIDCHR *));
  31.     ptrs[2] = calloc(r, sizeof(int));
  32.     ptrs[3] = calloc(r, sizeof(int));
  33.     
  34.     if (!ptrs[0] || !ptrs[1] || !ptrs[2] || !ptrs[3]) {
  35.         int n;
  36.  
  37.         for (n = 0; n < 4; n++)
  38.             if (ptrs[n])
  39.                 free(ptrs[n]);
  40.  
  41.         return NULL;
  42.     }            
  43.  
  44.     win = ptrs[0];
  45.     win->buf = ptrs[1];
  46.     win->firstx = ptrs[2];
  47.     win->lastx = ptrs[3];
  48.  
  49.     for (row = 0; row < r; row++) {
  50.         win->buf[row] = calloc(c, sizeof(VIDCHR));
  51.         if (!win->buf[row])
  52.             break;
  53.     }
  54.                 
  55.     if (row < r) {
  56.         int n;
  57.         
  58.         /* almost made it */
  59.         while (--row >= 0)
  60.             free(win->buf[row]);
  61.         for (n = 0; n < 4; n++)
  62.             free(ptrs[n]);
  63.         return NULL;
  64.     }
  65.                 
  66.     fillch.chr = ' ';
  67.     fillch.att = __DEFNORM;
  68.                 
  69.     for (row = 0; row < r; row++)
  70.         memsetw(win->buf[row], &fillch, c);
  71.                 
  72.     win->orgy = y;
  73.     win->orgx = x;
  74.     win->maxy = r - 1;
  75.     win->maxx = c - 1;
  76.     win->n_attr = win->attrib = __DEFNORM;
  77.     win->s_attr = __DEFSTAND;
  78.     win->b_attr = __DEFBRIGHT;
  79.     win->flags |= (_WWRAP | _WSCROLL);
  80.     if (r >= LINES && c >= COLS)
  81.         win->flags |= _WFULLWIN;
  82.     touchwin(win);
  83.  
  84.     return win;
  85. }
  86.  
  87. /*
  88.         r, c are scaled to fit if they are 0
  89.         y, x are relative to the screen, NOT the parent window
  90. */
  91.  
  92. WINDOW *
  93. subwin(win, r, c, y, x)
  94. WINDOW *win;
  95. {
  96.     WINDOW *sub;
  97.     int yoffs, xoffs, row, col;
  98.     void *ptrs[4];
  99.                 
  100.     yoffs = y - win->orgy;
  101.     xoffs = x - win->orgx;          
  102.  
  103.     if (yoffs < 0 || xoffs < 0) /* out of bounds */
  104.         return 0;
  105.     
  106.     row = win->maxy - yoffs + 1;
  107.     col = win->maxx - xoffs + 1;
  108.         
  109.     if (row < 1 || col < 1) /* out of bounds */
  110.         return 0;
  111.  
  112.     if (r == 0) /* set max rows */
  113.         r = row;
  114.     else if (r > row) /* too tall */
  115.         return 0;
  116.     if (c == 0) /* set max cols */
  117.         c = col;
  118.     else if (c > col) /* too wide */
  119.         return 0;
  120.  
  121.     ptrs[0] = calloc(1, sizeof(WINDOW));
  122.     ptrs[1] = calloc(r, sizeof(VIDCHR *));
  123.     ptrs[2] = calloc(r, sizeof(int));
  124.     ptrs[3] = calloc(r, sizeof(int));
  125.  
  126.     if (!ptrs[0] || !ptrs[1] || !ptrs[2] || !ptrs[3]) {
  127.         int n;
  128.             
  129.         for (n = 0; n < 4; n++)
  130.             if (ptrs[n])
  131.                 free(ptrs[n]);
  132.  
  133.         return  NULL;                    
  134.     }
  135.  
  136.     sub = ptrs[0];
  137.     sub->buf = ptrs[1];
  138.     sub->firstx = ptrs[2];
  139.     sub->lastx = ptrs[3];
  140.     
  141.     for (row = 0; row < r; row++)
  142.         sub->buf[row] = win->buf[row + yoffs] + xoffs;
  143.                 
  144.     sub->orgy = y;
  145.     sub->orgx = x;
  146.     sub->maxy = r - 1;
  147.     sub->maxx = c - 1;
  148.     sub->attrib = win->attrib;
  149.     sub->n_attr = win->n_attr;
  150.     sub->s_attr = win->s_attr;
  151.     sub->b_attr = win->b_attr;
  152.     sub->flags = win->flags | _WSUBWIN;
  153.     if (r < LINES || c < COLS)
  154.         sub->flags &= ~_WFULLWIN;
  155.     touchwin(sub);
  156.     
  157.     return sub;
  158. }
  159.